package com.sdy.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
mysql> desc position;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(64) | YES  |     | NULL    |                |
| level | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
 * */
public class Position extends Model<Position> {
	private static final long serialVersionUID = 4115202684078072009L;
	public static final Position dao = new Position();
	
	public Map<String, Object> datagrid(int page, int rows) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Position> positionList = null;
		Page<Position> positionPage = this.paginate(page, rows, "SELECT * ","FROM position order by level");
		if(positionPage != null)
			positionList = positionPage.getList();
		resultMap.put("rows", positionList);
		resultMap.put("total", this.find("select count(*) AS total from position").get(0).getLong("total"));
		return resultMap;
	}
	
	public Record getById(int id) throws Exception {
		List<Record> recordList = Db.find("select id 'position.id', name 'position.name', level 'position.level' from position where id = ?", id);
		return recordList.get(0);
	}
}
